#! /usr/bin/env python
# vi: set syntax=python : */

import gc

import findrox; findrox.version(1, 9, 8)
import rox

try:
	bool
except:
	# Python 2.2
	__builtins__.bool = lambda x: not not x

show_leaks = False

#rox.setup_app_options('Dome')

from rox.Menu import Menu, set_save_name
 
set_save_name('Dome')

no_gui_mode = 0

# Ugly hack to stop wierd chars appearing...
#import xml.dom.html
#xml.dom.html.HTML_CHARACTER_ENTITIES = {}

import sys
from os.path import dirname

app_dir = dirname(sys.argv[0])

from rox import g, mime
from rox.options import Option
from Window import Window
rox.setup_app_options('Dome')

default_font = Option('default_font', 'sans 12')

# All options must be registered by the time we get here
rox.app_options.notify()

#sys.argv.append('--disable-crash-dialog')
#import gnome.ui
#sys.argv.pop()

mime.install_mime_info('Dome-MIME')

mono = 0
while len(sys.argv) > 1 and sys.argv[1].startswith('--'):
	option = sys.argv[1][2:]
	del sys.argv[1]
	if option == 'mono':
		mono = 1		# Black & White
	elif option == '':
		break
	else:
		raise Exception("Unknown option", option)

files = sys.argv[1:]
del sys.argv[1:]		# GnomeCanvas can't cope with -

def go():
	apply(Window, files)

if show_leaks:
	# Run twice, once to make sure everything is initialised, and
	# again to make sure the memory use doesn't grow further.
	go()
	rox.mainloop()

	log = file('leak-log', 'w')

	gc.collect()
	old = {}
	for x in gc.get_objects():
		old[id(x)] = None
#import profile
#profile.run('go()')
go()

#profile.run('rox.mainloop()')
rox.mainloop()

if show_leaks:
	gc.collect()
	for x in gc.get_objects():
		if id(x) not in old:
			print `x`[:80]
			print>>log, "New %s: %s" % (type(x), `x`)
			for y in gc.get_referrers(x):
				if y is not globals() and id(y) in old:
					if type(y) == dict:
						print>>log, "\t(dict, key is %s)" % [k for k in y if y[k] is x]
					print>>log, "\t%s" % `y`
